package com.eastfair.pay.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.base.BaseControllerInfo;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.pay.demand.InvoiceApplyGroup;
import com.eastfair.pay.dto.InvoiceApplyDTO;
import com.eastfair.pay.dto.InvoiceApplyPageQuery;
import com.eastfair.pay.dto.InvoiceApplySaveDTO;
import com.eastfair.pay.dto.InvoiceApplyUpdateDTO;
import com.eastfair.pay.entity.InvoiceApply;
import com.eastfair.pay.service.InvoiceApplyService;
import com.eastfair.pay.vo.InvoiceApplyVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 发票申请
 * 目前不使用了
 * </p>
 *
 * @author clm
 * @date 2022-06-08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/invoiceApply")
@Api(value = "InvoiceApply", tags = "发票申请")
@PreAuth(replace = "pay:invoiceApply:")
public class InvoiceApplyController extends SuperController<InvoiceApplyService, Long, InvoiceApply, InvoiceApplyPageQuery, InvoiceApplySaveDTO, InvoiceApplyUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<InvoiceApply> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<InvoiceApply> invoiceApplyList = list.stream().map((map) -> {
            InvoiceApply invoiceApply = InvoiceApply.builder().build();
            //TODO 请在这里完成转换
            return invoiceApply;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(invoiceApplyList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<InvoiceApplyPageQuery> params) {
        InvoiceApplyPageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }
    /**
     * 发票申请保存
     */
    @PostMapping("/saveInvoiceApply")
    @BaseControllerInfo(voGroup = {InvoiceApplyGroup.invoiceApplyVOQueryById.class})
    public R saveInvoiceApply(@RequestBody  @Validated InvoiceApplyDTO invoiceApplyDTO){
        log.info("发票申请保存入参："+invoiceApplyDTO );
        try {
            baseService.saveInvoiceApply(invoiceApplyDTO);
            return success();
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * 发票管理查询
     *
     * @return
     */
    @PostMapping("/queryList")
    @BaseControllerInfo(voGroup = {InvoiceApplyGroup.invoiceApplyVOQueryPage.class})
    public R<Page<InvoiceApplyVO>> queryList(@RequestBody @Validated PageParams<InvoiceApplyPageQuery> params) {
        try {
            return R.success(baseService.queryList(params));
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * 开票详情查询
     * @param invoiceApplyId
     * @return
     */
    @GetMapping("invoiceApplyQueryById")
    @BaseControllerInfo(voGroup = {InvoiceApplyGroup.invoiceApplyVOQueryById.class})
    public R<InvoiceApplyVO>  invoiceApplyQueryById(@RequestParam("invoiceApplyId") @Validated @NotNull(message = "发票ID不能为为空") Long invoiceApplyId){
        log.info("结算详情查询 入参 invoiceApplyId {} ",invoiceApplyId);
        try {
            return  R.success(baseService.invoiceApplyQueryById(invoiceApplyId));
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }
    /**
     * 确认开票详情查询
     * @param invoiceApplyId
     * @return
     */
    @GetMapping("invoiceApplyCnfirmQueryById")
    @BaseControllerInfo(voGroup = {InvoiceApplyGroup.invoiceApplyCnfirmQueryById.class})
    public R<InvoiceApplyVO>  invoiceApplyCnfirmQueryById(@RequestParam("invoiceApplyId") @Validated @NotNull(message = "发票ID不能为为空") Long invoiceApplyId){
        log.info("结算详情查询 入参 invoiceApplyId {} ",invoiceApplyId);
        try {
            return  R.success(baseService.invoiceApplyQueryById(invoiceApplyId));
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * 取消发票申请.
     *
     * @param id the id
     * @return the r
     */
    @GetMapping("/cancel/{id}")
    public R<Boolean> cancelInvoiceApply(@PathVariable("id") Long id) {
        log.info("cancelInvoiceApply - 取消发票申请, id={}", id);
        return R.successDef(baseService.cancelInvoiceApply(id));
    }
}
